package com.hongshan.wallet.base

import android.app.Activity
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModel
import android.content.Context
import android.content.pm.ActivityInfo
import android.databinding.ViewDataBinding
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.util.InjectParams
import com.hongshan.wallet.util.MyActivityManager
import com.hongshan.wallet.widget.ProgressView
import kotlinx.android.synthetic.main.appbar.*
import me.goldze.mvvmhabit.base.BaseActivity
import me.goldze.mvvmhabit.base.BaseViewModel
import java.lang.reflect.ParameterizedType

/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.base
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */
abstract class MyActivity<V : ViewDataBinding,VM : MyViewModel>: BaseActivity<V,VM>() {

    val progress by lazy { AlertDialog.Builder(this).setView(ProgressView(this)).create() }

    val vm get() = viewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        MyActivityManager.addActivity(this)
//        ARouter.getInstance().inject(this)
        viewModel.finish.observe(this, Observer{ finish() })
        viewModel.activityResult.observe(this,Observer{
            intent.putExtras(it!!)
            val resultCode = it["RESULT_CODE"] as Int? ?: Activity.RESULT_OK
            setResult(resultCode,intent)
        })
        viewModel.showLoading.observe(this, Observer { showLoading() })
        viewModel.showSuccess.observe(this, Observer { showLoadSuccess() })
        viewModel.showEmpty.observe(this, Observer { showEmpty() })
        viewModel.showFail.observe(this, Observer { showLoadFailed() })
        ivBack?.setOnClickListener { finish() }
        InjectParams.inject(intent,viewModel,this)
        viewModel.initial()
        if (MyApplication.userInfo.get()?.account_online == "yes") {
            screenAlwaysOn()
        }
        progress.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    override fun onDestroy() {
        super.onDestroy()
        MyActivityManager.removeActivity(this)
        showLoadSuccess()
    }

    @Suppress("UNCHECKED_CAST")
    override fun initViewModel(): VM {
        if (viewModel == null) {
            val modelClass: Class<*>
            val type = javaClass.genericSuperclass
            if (type is ParameterizedType) {
                modelClass = type.actualTypeArguments[1] as Class<*>
            } else {
                //如果没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel::class.java
            }
            viewModel = createViewModel<ViewModel>(this, modelClass as Class<ViewModel>?) as VM
        }
        return viewModel
    }

    open fun showLoading() {
//        mHolder.showLoading()
        progress.show()
    }

    open fun showLoadSuccess() {
//        mHolder.showLoadSuccess()
        progress.dismiss()
    }

    open fun showLoadFailed() {
//        mHolder.showLoadFailed()
    }

    open fun showEmpty() {
//        mHolder.showEmpty()
    }

    open fun reLoad(){
        viewModel.reLoad()
    }

    open fun screenAlwaysOn(){
//        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    open fun cancleScreenOn(){
//        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN){
            ((getSystemService(Context.INPUT_METHOD_SERVICE)) as InputMethodManager?)?.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
        }
        return super.onTouchEvent(event)
    }
}